package com.news.service;

import com.news.db.OpDB;
import com.news.model.PageBean;
import com.news.model.Person;

import java.util.ArrayList;

/**
 * 分页展示新闻人物
 */
public class PersonService {

    public PageBean findPage(String rowsstr, String currentPagestr) {
        //1.创建一个空的PageBean对象
        PageBean pb = new PageBean();
        //2.创建一个dao接口
        OpDB opdb = new OpDB();
        //3.转型
        int rows = Integer.parseInt(rowsstr);
        int currentPage = Integer.parseInt(currentPagestr);
        //4.获取新闻人物数目
        int totalPage = opdb.OpCountPerson();
        //5.计算总页数
        int countPage = totalPage % rows == 0 ? totalPage / rows : totalPage / rows + 1;
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if (currentPage >= countPage) {
            currentPage = countPage;
        }

        //6.计算开始索引
        int index = (currentPage - 1) * rows;
        //7.调用dao方法
        ArrayList<Person> peoples = opdb.OpGetPersons(rows, index);

        //8.装载PageBean对象
        pb.setCountPage(countPage);
        pb.setCurrentPage(currentPage);
        pb.setList(peoples);
        pb.setRows(rows);
        pb.setTotalPage(totalPage);

        //9.返回PageBean对象
        return pb;
    }
}
